package com.lgq.cfw.controller.permission;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lgq.cfw.bean.TableCert;
import com.lgq.cfw.bean.message.Message;
import com.lgq.cfw.service.CertService;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author Lin
 * @date 2019-02-11 19:46
 **/
@RestController
@RequestMapping("/permission/cert")
public class CertController {
  @Autowired
  CertService certService;

  /**
   * 获取所有资质
   * @param currentPage 当前页数
   * @return 资质分页信息
   */
  @RequestMapping(method = RequestMethod.GET)
  public Message getAllCert(@RequestParam(value = "currentPage",defaultValue = "1")Integer currentPage){
    PageHelper.startPage(currentPage,10);
    List<TableCert> tableCertList=certService.getAllCert();
    PageInfo pageInfo=new PageInfo(tableCertList,5);
    return Message.success("处理成功",null).add("pageInfo",pageInfo);
  }

  @RequestMapping(value = "/add",method = RequestMethod.POST)
  public Message addCert(@RequestParam(value = "certName")String certName){
    boolean flag=certService.addCert(certName);
    if (flag){
      return Message.success("处理成功",null);
    }else {
      return Message.fail("处理失败",null);
    }
  }

  @RequestMapping(value = "/update",method = RequestMethod.POST)
  public Message updateCert(TableCert tableCert){
    boolean flag=certService.updateCert(tableCert);
    if (flag){
      return Message.success("处理成功",null);
    }else {
      return Message.fail("处理失败",null);
    }
  }

  /**
   * 批量和单个删除资质
   * @param certDeleteIdString 要删除的资质的id字符串
   * @return 处理信息
   */
  @RequestMapping(value = "/delete/{certDeleteIdString}",method = RequestMethod.DELETE)
  public Message delete(@PathVariable(value = "certDeleteIdString") String certDeleteIdString){
    boolean flag=certService.deleteCert(certDeleteIdString);
    if (flag) {
      return Message.success("处理成功", null);
    }else {
      return Message.fail("处理失败",null);
    }
  }

}
